package com.fjec.devops.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.util.SpecialMapUtil;
import com.fjec.devops.entity.BizFeedback;
import org.apache.commons.collections.MapUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fjec.devops.service.IBizFeedbackService;

import javax.annotation.Resource;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 004.意见反馈表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-09-18
 */
@RestController
@RequestMapping("/")
public class BizFeedbackController {

    @Resource
    private IBizFeedbackService service;

    /**
     * 发送反馈信息
     *
     * @param feedback
     * @return
     */
    @PostMapping("/y/feedback/send")
    public JsonResult sendFeedback(String feedback) {
        return service.sendFeedback(feedback);
    }

    /**
     * 查询反馈信息
     * @param page
     * @return
     */
    @GetMapping("/y/feedback/getList")
    public JsonResult getList(Page page) {
        List<Map<String, Object>> feedbacks = service.pageMaps(page, new QueryWrapper<BizFeedback>().orderByDesc("CreateTime")).getRecords();
        feedbacks.forEach(f -> {
            Integer busiType = MapUtils.getInteger(f, "BusiType");
            switch (busiType) {
                case 1:
                    f.put("busiType", "普通用户"); break;
                case 2:
                    f.put("busiType", "果农"); break;
                case 3:
                    f.put("busiType", "分拣厂"); break;
                case 4:
                    f.put("busiType", "农资厂"); break;
                case 5:
                    f.put("busiType", "包装物厂"); break;
                case 6:
                    f.put("busiType", "采购商"); break;
                default:

            }
        });

        List<Map<String, Object>> result = feedbacks.stream()
                .map(f -> SpecialMapUtil.lowKey(f))
                .collect(Collectors.toList());

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        result.forEach(m -> {
            LocalDateTime ldt = ((Timestamp) MapUtils.getObject(m,"createTime")).toLocalDateTime();
            String ct = ldt.format(formatter);
            m.put("createTime", ct);

        });
        return JsonResult.success()
                .data(result)
                .total(page.getTotal())
                .build();
    }
}
